package com.ijianji.module_play_video.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.fenghuajueli.lib_data.entity.db.VideoInfoEntity
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager
import com.ijianji.module_play_video.databinding.ItemRecommendGridVideoLayoutBinding
import com.ijianji.module_play_video.utils.RandomUtils

class VideoRecommendGridAdapter(val context: Context, var videoList: MutableList<VideoInfoEntity>) : RecyclerView.Adapter<VideoRecommendGridAdapter.ViewHolder>() {

    var onBaseItemClickListener: OnListClickListener<VideoInfoEntity>? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutInflater = LayoutInflater.from(context)
        val binding = ItemRecommendGridVideoLayoutBinding.inflate(layoutInflater, parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val entity = videoList[position]
        ImageLoadManager.loadForImageView(holder.binding.ivVideoPreviewImg, entity.img)
        holder.binding.tvVideoName.text = entity.title
        holder.binding.tvSeeCount.text = "${RandomUtils.getSeeCount(entity.vid)}人看过"
        holder.binding.root.setOnClickListener {
            onBaseItemClickListener?.itemClick(position, entity)
        }
    }

    override fun getItemCount(): Int {
        return videoList.size
    }


    fun refreshData(videoList: MutableList<VideoInfoEntity>) {
        this.videoList = videoList
        notifyDataSetChanged()
    }

    class ViewHolder(val binding: ItemRecommendGridVideoLayoutBinding) : RecyclerView.ViewHolder(binding.root)
}